// pages/products/products.js
var app = getApp();
var currentShopId = 0;
var currentPage = 1;

Page({
  data:{
    hideCartAS: true,
    total: 0,
    cart_count: 0,
    categories_actice: 0,
    categories: [],
    products: [],
    carts: [],
    curProduct: {},
    showModal: false,
    loadMore: true
  },
  onLoad:function(options){
    // 页面初始化 获取店铺id
    console.log(options.shop_id)
    if (options.shop_id > 0) {
      currentShopId = options.shop_id;
      wx.setStorageSync('shop_id', options.shop_id);
    } else {
      currentShopId = wx.getStorageInfoSync('shop_id');
    }
  },
  onReady:function(){
    // 页面渲染完成
    var that = this;
    // 缓存中读取购物车的信息
    if (that.data.products.length == 0) {
      currentPage = 1;
    }
    //读取分类列表

    app.func.req('product_cates/index', {shop_id: currentShopId}, 'GET', function(res){
      var index = that.data.categories_actice;
      var cateId = res.result[0].id;
      var categories =  app.util.covertProductCates(res.result);
      categories[0].active = true;
      if (that.data.categories.length>0) {
        var oldCategories = that.data.categories;
        var oldCateId = oldCategories[index].id;

      for (var i = 0; i < categories.length;i++){
        if (categories[i].id == oldCateId) {
            categories[i].active = true;
            cateId = oldCateId;
        }else{
          categories[i].active = false;
        }
      }
      }
      that.setData({
        categories: categories
      });
      // 获取该分类下的商品列表
      that.getproducts(cateId);
    });

    app.func.req('shops/show', {
        id: currentShopId
      }, 'GET', function(res){
        that.setData({
          shop: res.result
        });
        var title = '店铺首页';
        if (res.result.name) {
          title = res.result.name;
        }
        wx.setNavigationBarTitle({
          title: title
        })
      },{
        'token': wx.getStorageSync('token')
      });
  },
  //获取当前当前店铺指定分类下的商品列表
  getproducts: function(cateId){

    console.log("currentPage:" + currentPage);
    var that = this;
    that.setData({
      loadMore: false
    })
    wx.showLoading({ title: '数据加载中...', mask: true });
    app.func.req('products/index', {
      shop_id: currentShopId,
      product_cate_id: cateId,
      page_index: currentPage,
      page_size: 10
    }, 'GET', function(res){
      console.log(res)
      if (res.code == "success") {
      var products = app.util.convertProducts(res.result);
      var currentProducts = that.data.products.concat(products);
      that.setData({
        loadMore: true
      });
      //购物车信息加到商品中
      that.checkCarts(that.data.carts, currentProducts);
      currentPage++;
      }
      wx.hideLoading();
    },{
      'token': wx.getStorageSync('token')
    });
  },
  loadMore: function(){
    var that = this;
    if (that.data.loadMore) {
      that.getproducts(that.data.categories[that.data.categories_actice].id);
    }
  },
  //分类切换
  categorySelect: function(e){
    var that = this;
    var index = e.target.dataset.index;
    var new_categories = this.data.categories;
    var old_active = this.data.categories_actice;
    var new_active = index;
    new_categories[old_active].active = false;
    new_categories[index].active = true;
    this.setData({
      categories: new_categories,
      categories_actice: new_active,
      products: []
    })
    //获取商品列表
    currentPage = 1;
    that.getproducts(new_categories[index].id);
  },
  closeModal: function(){
    this.setData({
      showModal: false
    })
  },
  onShareAppMessage: function () {
    var nickname = wx.getStorageSync('nickname');
    if (!nickname) {
      nickname = ''
    }

    return {
      title: nickname + '分享了' + this.data.shop.name,
      imageUrl: this.data.shop.logo,
      path: 'pages/share/share?shop_id=' + currentShopId + '&&products_share=true'
    }
  }
})
